// 舞台事件(全局事件)
import { cutBtnMouseDown, creationMouseDown, mousedownBottom, mousedownDiyPciture } from './indiv'
import { createEf } from '@renderer/utils/ef'
import useMagic from '@renderer/store/modules/useMagicStore'
import useCardsStore from '@renderer/store/modules/useCardsStore'
import useDiyStore from '@renderer/store/modules/useDiyStore'
import { bottomParams, stageParam, diyParams } from './params'
import './watch'
import { getPixi } from '@renderer/store/modules/usePixiStore'

const cardsStore = useCardsStore()
const diyStore = useDiyStore()

// 全局的鼠标按下事件
export const stageMouseDown = (() => {
  return createEf(
    (event) => {
      stageParam.downUid = event.target.uid
      stageMousemove.on()
    },
    {
      type: 'mousedown',
      finish: () => {
        stageParam.downUid = -1
        stageMousemove.off()
      },
    },
  )
})()
/** 全局的鼠标移动事件*/
export const stageMousemove = (() => {
  return createEf(
    (event) => {
      if (bottomParams.bottomMouseDown && bottomParams.quiet) {
        const cardSetsContainer = getPixi('cardSets')
        cardSetsContainer.position.y = bottomParams.origin.positionY + event.clientY - bottomParams.origin.y
      }
      if (diyParams.onMouseDown) {
        const container = getPixi(diyStore.imageUid)
        const sprite = container.children[0]
        const bounds = sprite.getBounds()
        const realScale = bounds.width / diyStore.creationData.transform.realWidth / sprite.scale.x
        const px = diyParams.origin.positionX + (event.clientX - diyParams.origin.x) / realScale
        const py = diyParams.origin.positionY + (event.clientY - diyParams.origin.y) / realScale
        if (Math.abs(px) <= (sprite.width - useMagic().PICTURE_SCOPE) / 2) {
          sprite.position.x = px
        }
        if (Math.abs(py) <= (sprite.height - useMagic().PICTURE_SCOPE) / 2) {
          sprite.position.y = py
        }
      }
    },
    {
      type: 'mousemove',
      activate: false,
    },
  )
})()
// 全局的鼠标抬起事件
export const stageMouseUp = (() => {
  return createEf(
    (event) => {
      // 切换按钮
      if (bottomParams.onCutBtn === true) {
        cutBtnMouseDown
          .finish(event, stageParam.downUid)
          .then(() => {
            cardsStore.changeFrom()
          })
          .catch(() => {})
      }
      // 创作按钮
      if (bottomParams.onCreation === true) {
        creationMouseDown
          .finish(event, stageParam.downUid)
          .then(() => {
            diyStore.reset()
            diyStore.show()
          })
          .catch(() => {})
      }
      // 判断是否是在卡册内的拖动
      if (bottomParams.bottomMouseDown && bottomParams.quiet) {
        mousedownBottom.finish(event).catch(() => {})
      }
      // diy图片拖动
      if (diyParams.onMouseDown === true) {
        mousedownDiyPciture.finish(event, stageParam.downUid).catch(() => {})
      }
      stageMouseDown.finish(event).catch(() => {})
    },
    {
      type: 'mouseup',
    },
  )
})()
/** 鼠标离开页面 */
export const stageMouseLeave = (() => {
  return createEf(
    (event) => {
      // 切换按钮
      if (bottomParams.onCutBtn === true) {
        cutBtnMouseDown.finish(event).catch(() => {})
      }
      // 创作按钮
      if (bottomParams.onCreation === true) {
        creationMouseDown.finish(event).catch(() => {})
      }
      // 判断是否是在卡册内的拖动
      if (bottomParams.bottomMouseDown && bottomParams.quiet) {
        mousedownBottom.finish(event).catch(() => {})
      }
      // diy图片拖动
      if (diyParams.onMouseDown === true) {
        mousedownDiyPciture.finish(event).catch(() => {})
      }
      stageMouseDown.finish(event).catch(() => {})
    },
    {
      type: 'mouseleave',
    },
  )
})()
